package com.smart.wap.service;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import com.smart.manager.member.entities.bodys.RMemberMaterialProps;
import com.smart.manager.member.entities.enums.CoinAccountEvent;
import com.smart.manager.member.entities.enums.EnergyAccountEvent;
import com.smart.manager.member.entities.models.MemberProfileAccount;
import com.smart.manager.member.entities.views.MemberView;
import com.smart.manager.member.repository.MemberAccountRepository;
import com.smart.manager.member.repository.MemberMaterialPropsRepository;
import com.smart.manager.pop.entities.models.PopBasicDiamond;
import com.smart.manager.pop.entities.models.PopBasicDiamondLogs;
import com.smart.manager.pop.entities.models.query.QPopBasicDiamond;
import com.smart.manager.pop.entities.models.query.QPopBasicDiamondLogs;
import com.smart.manager.product.entities.models.PropsServe;
import com.smart.manager.product.entities.models.PropsSnacks;
import com.smart.manager.product.entities.models.query.QPropsServe;
import com.smart.manager.product.entities.models.query.QPropsSnacks;
import com.smart.utils.MemberAcl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class DiamondService {

    private final MemberAccountRepository memberAccountRepository;
    private final MemberMaterialPropsRepository memberMaterialPropsRepository;

    /**
     * 获取所有礼包
     *
     * @return {@link List}<{@link PopBasicDiamond}>
     */
    public List<PopBasicDiamond> getDiamondList() {
        return new QPopBasicDiamond().order().sort.asc().findList();
    }


    /**
     * 兑换礼包
     *
     * @param code
     */
//    @Transactional(rollbackFor = Exception.class)
    public void addDiamond(String code) {
        log.warn("兑换礼包:{}", code);
        Long loginId = MemberAcl.getLoginId();
        PopBasicDiamond diamond = new QPopBasicDiamond().code.eq(code).setMaxRows(1).findOne();
        Assert.isFalse(ObjUtil.isEmpty(diamond), "礼包不存在!");
        MemberProfileAccount account = memberAccountRepository.getAccount(loginId);
        BigDecimal decimal = NumberUtil.sub(account.getMedalBalance(), diamond.getPrice());
        Assert.isFalse(decimal.compareTo(BigDecimal.ZERO) < 0, "钻石不足!");
        //更新钻石
        account.setMedalBalance(decimal.intValue()).update();
        //添加流水
        PopBasicDiamondLogs diamondLog = BeanUtil.copyProperties(diamond, PopBasicDiamondLogs.class);
        diamondLog.setId(null);
        diamondLog.setMember(new MemberView().setId(loginId)).insert();
        //加入背包
        RMemberMaterialProps.EditBody editBody = new RMemberMaterialProps.EditBody()
                .setName(diamond.getName())
                .setIcon(diamond.getIcon())
                .setCode(diamond.getCode())
                .setNumber(1)
                .setMember(new MemberView().setId(loginId))
                .setKind(diamond.getKind())
                .setDurable(0);


        switch (diamond.getKind()) {
            case L1:
                PropsSnacks snacks = new QPropsSnacks().code.eq(diamond.getDataCode()).setMaxRows(1).findOne();
                editBody.setIcon(snacks.getIcon()).setName(snacks.getName()).setCode(snacks.getCode());
                memberMaterialPropsRepository.add(editBody);
                break;
            case L2:
            case L3:
                PropsServe serve1 = new QPropsServe().code.eq(diamond.getDataCode()).setMaxRows(1).findOne();
                editBody.setIcon(serve1.getIcon()).setName(serve1.getName()).setCode(serve1.getCode());
                memberMaterialPropsRepository.add(editBody);
                break;
            case L6:
                exchangeEnergy(loginId, diamond);
                break;
            case L7:
                exchangeGold(loginId, diamond);
                break;
            case L8:
                exchangeSteal(loginId, diamond);
                break;
        }
    }


    /**
     * 兑换能量
     *
     * @param id
     */
    public void exchangeEnergy(Long id, PopBasicDiamond diamond) {

        memberAccountRepository.savePowerEvent(id, EnergyAccountEvent.DIAMOND, diamond.getQuantity(), "钻石兑换");

    }

    /**
     * 兑换次数
     * @param id
     * @param diamond
     */
    public void exchangeSteal(Long id, PopBasicDiamond diamond) {
            memberAccountRepository.saveStealingEggs(id, diamond.getQuantity());
    }

    public void exchangeGold(Long id, PopBasicDiamond diamond) {

        memberAccountRepository.saveEvent(id, CoinAccountEvent.DIAMOND, diamond.getQuantity(), "钻石兑换");

    }


    public List<PopBasicDiamondLogs> getDiamondLogList(Long id) {
        return new QPopBasicDiamondLogs().member.id.eq(id).createTime.desc().findList();
    }
}
